/**
 * 制作者：九哥
 * 消息数据对接
 * 时间：2019-03-01
 * 更新者：
 * */
var messageListDetailUrl = '/h5/message/detail.html';

function messageCount()
{
    var userInfo = JSON.parse(localStorage.getItem('userInfo'));
    var app_access = userInfo.app_access;
    var token = userInfo.token;
    var data = {
        app_access: app_access,
        token: token
    }
    if (!userInfo) {
        alertMsg('请先登录');
    } else {
        $.ajax({
            url: messageCountUrl,
            data: data,
            datatype: 'json',
            type: 'get',
            cache: true,
            success: function(data) {
                if(data.data.company > 0){
                    $('.company_count').show();
                }
                if(data.data.factory > 0){
                    $('.factory_count').show();
                }
                if(data.data.job > 0){
                    $('.job_count').show();
                }
                if(data.data.plat > 0){
                    $('.plat_count').show();
                }
                if(data.data.inside > 0){
                    $('.inside_count').show();
                }
                if(data.data.system > 0){
                    $('.system_count').show();
                }
                if(data.data.zhun > 0){
                    $('.zhun_count').show();
                }
                $('.company_count').html(data.data.company);
                $('.factory_count').html(data.data.factory);
                $('.job_count').html(data.data.job);
                $('.plat_count').html(data.data.plat);
                $('.inside_count').html(data.data.inside);
                $('.system_count').html(data.data.system);
                $('.zhun_count').html(data.data.zhun);
            },
            error:function(){
                getErrMsg('操作错误！');
            }
        });
    }
   
}

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if ( r != null ){
       return unescape(r[2]);
    }else{
       return null;
    } 
 }

function messageList(type) {
    var userInfo = JSON.parse(localStorage.getItem('userInfo'));
    var app_access = userInfo.app_access;
    var type = getQueryString('type');
    var token = userInfo.token;
    var data = {
        app_access: app_access,
        token: token, 
        type: type
    }
    $.ajax({
        url: messageListUrl,
        data: data,
        datatype: 'json',
        type: 'get',
        cache: true,
        success: function(data) {
            if (data.code === 200) {
                data = data.data;
                if (data.length > 0) {
                    html = '';
                    for (var i = 0; i < data.length; i++) {

                        html += "<a href="+ messageListDetailUrl + '?id=' + data[i].id + " class=\"aui-six-list-item\">"+
                                    "<div class=\"aui-flex b-line\">"+
                                        "<div class=\"aui-flex-box\">"+
                                            "<h3>"+ data[i].title +"</h3>"+
                                            "<h2>"+ data[i].content +"</h2>"+
                                        "</div>"+
                                    "</div>"+
                                    "<div class=\"aui-bottom-text\">"+
                                        "<p>"+ data[i].date +"</p>"+
                                    "</div>"+
                                "</a>";

                    }
                    $('.message_content').html(html);
                } else {
                    $('.message_content').html('<div class="nullCetner">暂无消息</div>');
                }
            }
            
            
        },
        error:function(){
            getErrMsg('操作错误！');
        }
    });
}

function messageDetail() {
    var userInfo = JSON.parse(localStorage.getItem('userInfo'));
    var app_access = userInfo.app_access;
    var id = getQueryString('id');
    var token = userInfo.token;
    var data = {
        app_access: app_access,
        token: token, 
        id: id
    }
    $.ajax({
        url: messageListUrl,
        data: data,
        datatype: 'json',
        type: 'get',
        cache: true,
        success: function(data) {
            if (data.code === 200) {
                data = data.data;
                html = '';
                html += `
                <a href="javascript:;" class="aui-six-list-item">
                    <div class="aui-flex">
                        <div class="aui-flex-box">
                            <h3>${data.title}</h3>
                            <p>${data.date}</p>
                            <div style="color:#1F3858;margin-top: 10px;">
                                ${data.content}
                            </div>
                        </div>
                    </div>
                </a>
                `;
                $('.message_content').html(html);
            }
            
            
        },
        error:function(){
            getErrMsg('操作错误！');
        }
    });
}
/**
 * 其他自定义
 * */





/**
 * 错误提示框
 * */
function getErrMsg(msg){
    var h = '';
    h += '<span>'+msg+'</span>';
    h += '<script>setTimeout("setErrClose()",1000*3); function setErrClose(){ $("#errMsg").html("").hide(); }</script>';
    $('#errMsg').html(h).show();
}